import React from 'react';
import {Spin, Card, Button, Table, Row, Col, Checkbox} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {transportType, cabinetType, palletizing} from '../Enum';
import routes from './routes';
import {DATETIME_FORMAT, TABLE} from '../constants';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {getDetail, sapData} from './api';
import {injectIntl} from './intl';
import styles from './style.css';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import WrappedPopconfirm from '../common/WrappedPopconfirm';

const {Description} = DescriptionList;
class DetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: []
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        details: res.data.details
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }
    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    onClickSap = () => {
        this.setState({loading: true});
        const data = {
            rowVersion: this.state.data.rowVersion
        };
        sapData(this.state.data.id, data)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    render() {
        const {data} = this.state;
        const {formatMessage} = this.props.intl;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.sap) && hasPermissions(data.options, PERMISSION.sap))
            operations.push(<WrappedPopconfirm
                key="sap"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickSap}
                title={formatMessage({
                    id: 'detailPanel.validate.sap',
                    defaultMessage: '是否确认上传？'
                })}>
                <Button key="sap" size="small">{
                    formatMessage({
                        id: 'detailPanel.sap',
                        defaultMessage: '上传SAP'
                    })
                }</Button>
            </WrappedPopconfirm>);
        const colums = [{
            title: formatMessage({
                id: 'detailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.planQuantity',
                defaultMessage: '订货数量'
            }),
            dataIndex: 'planQuantity'
        }, {
            title: formatMessage({
                id: 'detailPanel.column.businessCheck',
                defaultMessage: '商检'
            }),
            dataIndex: 'businessCheck',
            render: text => text ? formatMessage({
                id: 'detailPanel.column.yes',
                defaultMessage: '是'
            }) : formatMessage({
                id: 'detailPanel.column.no',
                defaultMessage: '否'
            })
        }, {
            title: formatMessage({
                id: 'detailPanel.column.airMagneticCheck',
                defaultMessage: '空运磁检'
            }),
            dataIndex: 'airMagneticCheck',
            render: text => text ? formatMessage({
                id: 'detailPanel.column.yes',
                defaultMessage: '是'
            }) : formatMessage({
                id: 'detailPanel.column.no',
                defaultMessage: '否'
            })
        }, {
            title: formatMessage({
                id: 'detailPanel.column.dangerousGoods',
                defaultMessage: '空运危险'
            }),
            dataIndex: 'dangerousGoods',
            render: text => text ? formatMessage({
                id: 'detailPanel.column.yes',
                defaultMessage: '是'
            }) : formatMessage({
                id: 'detailPanel.column.no',
                defaultMessage: '否'
            })
        }, {
            title: formatMessage({
                id: 'detailPanel.column.chemicalCheck',
                defaultMessage: '化工鉴定'
            }),
            dataIndex: 'chemicalCheck',
            render: text => text ? formatMessage({
                id: 'detailPanel.column.yes',
                defaultMessage: '是'
            }) : formatMessage({
                id: 'detailPanel.column.no',
                defaultMessage: '否'
            })
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList title={<Row><Col span={6}>{
                            formatMessage({
                                id: 'detailPanel.card.common',
                                defaultMessage: '订单基本信息'
                            })
                        }</Col>
                        <Col className={`col-align-right ${styles.detail}`} span={18}>{operations}</Col></Row>}>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.dealerCode',
                                defaultMessage: '总代编号'
                            })}>{data.dealerCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.dealerName',
                                defaultMessage: '总代名称'
                            })}>{data.dealerName}</Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.orderCode',
                                defaultMessage: '销售订单编号'
                            })}>{data.orderCode}</Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.piCode',
                                defaultMessage: 'PI编号'
                            })}>{data.piCode}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.partOrderTypeName',
                                defaultMessage: '订单类型'
                            })}>{data.partOrderTypeName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.submitTime',
                                defaultMessage: '提报日期'
                            })}>{formatDateTime(data.submitTime, DATETIME_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.remark',
                                defaultMessage: '备注'
                            })}>{data.remark}</Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'detailPanel.table.details',
                            defaultMessage: '清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={colums}
                                rowKey="partId"
                                pagination={false}
                                dataSource={this.state.details}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={<Row><Col span={6}>{
                            formatMessage({
                                id: 'detailPanel.card.packing',
                                defaultMessage: '包装信息'
                            })
                        }</Col></Row>}>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.transportType',
                                defaultMessage: '运输方式'
                            })}>{conventEnumValueToString(transportType, data.transportType)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.labelRequirement',
                                defaultMessage: '标签要求'
                            })}>{data.labelRequirement}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.shippingMark',
                                defaultMessage: '唛头要求'
                            })}>{data.shippingMark}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.isEngine',
                                defaultMessage: '包含发动机'
                            })}>
                                <Checkbox
                                    disabled={true}
                                    checked={data.isEngine}
                                    name="isEngine"
                                    value={data.isEngine}>
                                </Checkbox>{data.isEngine}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.isDangerousGoods',
                                defaultMessage: '包含空运危险物'
                            })}>{data.isDangerousGoods}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.isAirMagneticCheck',
                                defaultMessage: '包含磁检'
                            })}>{data.isAirMagneticCheck}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.isChemicalCheck',
                                defaultMessage: '包含化工鉴定'
                            })}>{data.isChemicalCheck}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.cabinetType',
                                defaultMessage: '柜形'
                            })}>{conventEnumValueToString(cabinetType, data.cabinetType)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.palletizing',
                                defaultMessage: '托盘要求'
                            })}>{conventEnumValueToString(palletizing, data.palletizing)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.isSeparate',
                                defaultMessage: '商检'
                            })}>
                                <Checkbox
                                    disabled={true}
                                    checked={data.isSeparate}
                                    name="isSeparate"
                                    value={data.isSeparate}>
                                </Checkbox>{data.isSeparate}
                            </Description>
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{this.props.intl.formatMessage({
                        id: 'detailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};

import {connect} from 'react-redux';
import {tableSearch} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
